package datatableaux;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

import entities.Assertion;
import entities.ConceptAssertion;
import entities.Individual;
import entities.RoleAssertion;
import entities.concept.Concept;
import entities.role.Role;
import kb.*;


/**
 * A data graph is built upon the initial ABox, and is associated with that ABox.
 * For TBox subsumption check only, this should be empty.
 * Nodes are individuals and literals, while edges are roles.
 * @author j55wu
 *
 */
public class DataGraph {
	private ABox ab;
	private DataCompletion dcomp;
	
	public DataGraph(DataCompletion dc, ABox ab){
		this.dcomp = dc;
		this.ab = ab;
	}
	
	public ABox getAssociatedABox(){
		return this.ab;
	}
	
	public DataCompletion getAssociatedCompletion(){
		return this.dcomp;
	}
	
	//no edges, only isolated nodes
	public boolean simpleDataGraph(){
		return this.ab.isRoleAssertionFreeABox();
	}
	
//	public void ABox2DataGraph(){
//		HashMap<Individual, HashMap<Role, HashSet<Individual>>> roleAssertions = this.ab.getRoleAssertions();
//		HashMap<Individual,Concept> conceptAssertions = this.ab.getConceptAssertions();
//		//add role assertions
//		for(Individual ind:roleAssertions.keySet()){
//			HashMap<Role, HashSet<Individual>> rolefiller = roleAssertions.get(ind);
//			for(Role r: rolefiller.keySet()){
//				ind.addSucessors(r, rolefiller.get(r));
//				//ancestors are added automatically			
//			}
//		}
//		//add concept labels for every ind
//		for(Individual ind:conceptAssertions.keySet()){
//			//ind.addLabels(preprocessor.Simplification.NNF(conceptAssertions.get(ind), false));
//			ind.addLabels(conceptAssertions.get(ind));
//			System.out.println(ind+ "==datagraph====>" + ind.getLabels());
//		}	
//		return ;
//	}
	
	public void ABox2DataGraph(){
		HashMap<Individual, HashMap<Role, HashSet<Individual>>> roleAssertions = this.ab.getRoleAssertions();
		HashMap<Individual,Concept> conceptAssertions = this.ab.getConceptAssertions();
		//for(Individual ind:Individual.getAllIndividuals().values()){
		for(Individual ind:this.ab.getABoxIndividuals()){
			if(roleAssertions.keySet().contains(ind)){
				HashMap<Role, HashSet<Individual>> rolefiller = roleAssertions.get(ind);
//				for(Role r: rolefiller.keySet()){
//					ind.addSucessors(r, rolefiller.get(r));
//					//ancestors are added automatically			
//				}
				for(Entry<Role, HashSet<Individual>> e:rolefiller.entrySet()){
					Role r = e.getKey();
					ind.addSucessors(r, e.getValue());
					//System.out.println("add: ("+ind+","+e.getValue()+"):"+r);
				}
			}
			if(conceptAssertions.keySet().contains(ind)){
				ind.addLabels(conceptAssertions.get(ind));
				//System.out.println(ind+ "==datagraph====>" + ind.getLabels());
			}
		}
		return ;
	}
	
	public void addNewAssertion(Assertion as){
		if(as instanceof ConceptAssertion){
			ConceptAssertion ca = (ConceptAssertion) as;
			Individual ind = ca.getIndividual();
			Concept c = ca.getConcept();
			ind.addLabels(c);
			this.ab.insertConceptAssertions(ca);
		}else{
			RoleAssertion ra = as.asRoleAssertion();
			Individual a = ra.getFirstIndividual(), b = ra.getSecondIndividual();
			Role r = ra.getRole();
			a.addSucessors(r, b);
			this.ab.insertAssertions(ra);
		}
	}
	
	
	

}
